<template>
  <div>
    <a-modal title="剂型" :width="900" :visible="visible" @cancel="hideModal">
      <a-table :scroll="{ x: true }" ref="table" bordered :dataSource="dataSource" :columns="columns"
        :rowKey="(record,index)=> index">
        <div slot="action" slot-scope="text, record" href="javascript:;">
          <a-checkbox v-model="record.checked"></a-checkbox>
        </div>
      </a-table>
      <template #footer>
        <a-button key="back" @click="hideModal">取消</a-button>
        <a-button key="submit" type="primary" :loading="loading" @click="submit">修改</a-button>
      </template>
    </a-modal>
  </div>
</template>

<script>
  import {
    pharmacyjixing,
    pharmacyJixingUpdate
  } from '@/api/pharmacys'
  export default {
    data() {
      return {
        id: 0,
        loading: false,
        visible: false,
        dataSource: [],
        columns: [{
            title: 'id',
            dataIndex: 'id',
            key: 'id',
            width: 100,
          },
          {
            title: '名称',
            dataIndex: 'name',
            key: 'name',
            ellipsis: true,
          },
          {
            title: '选择',
            dataIndex: 'checked',
            key: 'checked',
            scopedSlots: {
              customRender: 'action'
            }
          }
        ],
      }
    },
    methods: {
      submit() {
        this.loading = true
        let list = this.dataSource.map(res => {
          return res.checked ? res.id * 1 : ''
        })
        list = list.filter(Boolean)
        pharmacyJixingUpdate({
          id: this.id,
          jixingids: list.length > 0 ? list.join(',') : ''
        }).then((res) => {
          if (res.code == 0) {
            this.$emit('ok')
            this.loading = false
            this.visible = false
          }
        })
      },
      hideModal() {
        this.visible = false
        this.dataSource = []
      },
      initData(v) {
        this.id = v.id
        pharmacyjixing({
          id: v.id
        }).then(res => {
          this.visible = true
          res.data.forEach(v => {
            v.checked = v.xuanzhong == 2 ? true : false
          })
          this.dataSource = res.data
        })
      }
    }
  }
</script>

<style>
</style>